//
//  SLDatePickerView.m
//  SulitLoanDemo
//
//  Created by 虞嘉伟 on 2025/2/22.
//

#import "SLDatePickerView.h"
#import <YYKit.h>
#import <Masonry.h>
#import <PGDatePicker.h>


@interface SLDatePickerView ()
@property (nonatomic, strong) PGDatePicker *datePicker;
@end

@implementation SLDatePickerView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self.customView addSubview:self.datePicker];
        [self.datePicker mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.customView);
        }];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    [self.customView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(251);
    }];
}

#pragma mark - 点击事件
- (void)confirmButtonAction:(UIButton *)button {
    [super confirmButtonAction:button];
    [self.datePicker tapSelectedHandler];
}

- (PGDatePicker *)datePicker {
    if (!_datePicker) {
        _datePicker = [PGDatePicker new];
        _datePicker.datePickerMode = PGDatePickerModeDate;
        _datePicker.rowHeight = 30;
        _datePicker.language = @"en";
        [_datePicker setDate:NSDate.date animated:YES];
        _datePicker.textColorOfSelectedRow = UIColorHex(#333333);
        _datePicker.textFontOfSelectedRow = aspectDefaultFont(14);
        _datePicker.textColorOfOtherRow = UIColorHex(#999999);
        _datePicker.textFontOfOtherRow = aspectDefaultFont(14);
        _datePicker.lineBackgroundColor = UIColor.redColor;
        @weakify(self);
        _datePicker.selectedDate = ^(NSDateComponents *dateComponents) {
            @strongify(self);
            NSCalendar *calendar = NSCalendar.currentCalendar;
            NSDate *date = [calendar dateFromComponents:dateComponents];
            !self.confirmButtonCallback ?: self.confirmButtonCallback(date);
        };
    }
    return _datePicker;
}


@end



@implementation SLDatePickerView (Fast)

+ (instancetype)showDateChooseWithDefaultDate:(NSDate *)date confirmCallback:(void(^)(NSDate *select))block {
    SLDatePickerView *alert = [SLDatePickerView new];
    alert.titleLabel.text = @"Select Date";
    alert.confirmButtonCallback = block;
    [alert.datePicker setDate:date animated:YES];
    [UIApplication.sharedApplication.windows.firstObject addSubview:alert];
    [alert mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(alert.superview);
    }];
    
    return alert;
}

@end
